package com.jjshop.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import com.jjshop.utils.GlobalContanst;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by ysx on 2018/5/19.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{

    private static final String TAG = WXPayEntryActivity.class.getSimpleName();

    //private TextView mTv;
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_pay_result);

        //mTv = (TextView) findViewById(R.id.tv_test);

        api = WXAPIFactory.createWXAPI(this, GlobalContanst.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }


    @Override
    public void onResp(BaseResp baseResp) {
        int errCode = baseResp.errCode;
        Log.d(TAG, "errCode--->" + errCode);
        String result = "";
        switch (errCode) {
            case 0: // 支付成功
                result = "支付成功";
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(GlobalContanst.ACTION_PAY_RESULT));
                break;
            case -1: // 支付失败
                result = "支付失败，请重试";
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(GlobalContanst.ACTION_PAY_RESULT));
                break;
            case -2: // 用户取消支付
                result = "您取消了支付";
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(GlobalContanst.ACTION_PAY_RESULT));
                break;
        }

        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        finish();
    }
}
